﻿
Partial Class WebForm_ManageRoleUserMappingByUser_AssignRole
    'Inherits System.Web.UI.UserControl
    Inherits My.MyUserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLOrganization()
            Me.BindDDLUserType()
            RaiseEvent Loaded(Me, New EventArgs)
        End If
    End Sub

    Protected Sub ddlOrganization_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrganization.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub ddlUserType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUserType.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

        If e.CommandName = "btnAddRole" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            Me.SelectedRowIndex = row.RowIndex
            Me.SearchRoleControl1.Show(key("UserID"))
        End If
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        If Me.DeleteData(e.RowIndex) = True Then
            Me.BindGV()
        End If
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim key As DataKey = Me.gv.DataKeys(e.Row.RowIndex)

            Dim isRole As Boolean = key("RoleID") <> -1

            If isRole = True Then
                e.Row.BackColor = Me.gv.AlternatingRowStyle.BackColor
                TryCast(e.Row.FindControl("MultiView1"), MultiView).SetActiveView(TryCast(e.Row.FindControl("DeleteButtonView"), View))

                If DataBinder.Eval(e.Row.DataItem, "RoleName") <> "System" AndAlso TypeOf DataBinder.Eval(e.Row.DataItem, "RoleParent") Is DBNull Then
                    TryCast(e.Row.FindControl("btnDeleteRole"), Button).Enabled = False
                End If
            Else
                e.Row.BackColor = Me.gv.RowStyle.BackColor
                e.Row.Font.Bold = True
                TryCast(e.Row.FindControl("MultiView1"), MultiView).SetActiveView(TryCast(e.Row.FindControl("AddButtonView"), View))
            End If
        End If
    End Sub

    'Protected Sub gv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.SelectedIndexChanged
    '    Dim row As GridViewRow = Me.gv.SelectedRow
    '    Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

    '    If Me.DataAccessMode = My.DataAccessMode.Update Then
    '        Me.SearchRoleUserMappingByUser1.Show(key("UserID"))
    '    Else
    '        Me.SearchRoleUserMappingByUser1.ShowReadOnly(key("UserID"))
    '    End If

    'End Sub

    Protected Sub SearchRoleControl1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchRoleControl1.Saving
        Me.SaveData(Me.SelectedRowIndex)
        Me.BindGV()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub
End Class

Partial Class WebForm_ManageRoleUserMappingByUser_AssignRole
    Private Sub BindDDLOrganization()
        'Dim dat As New clsData
        'Dim sql As New MyStringBuilder
        'Dim dt As Data.DataTable

        'sql.Length = 0
        'sql.AppendFormat("select ua.*, o.OrganizationName, UOrganizationID ")
        'sql.AppendFormat("from UserOrganizationAuth ua ")
        'sql.AppendFormat("inner join OrganizationView o on ua.OrganizationID = o.OrganizationID and ua.VOrganizationID = o.VOrganizationID ")
        'sql.AppendFormat("where ua.UserID = '{0}' ", clsAuth.GetUserID())
        'sql.AppendFormat("order by o.VOrganizationCode, o.OrganizationCode ")

        'dt = dat.GetData(sql)

        Me.ddlOrganization.DataTextField = "OrganizationName"
        Me.ddlOrganization.DataValueField = "UOrganizationID"
        Me.ddlOrganization.DataSource = MyObj.GetDDLOrganizationData()
        Me.ddlOrganization.DataBind()

    End Sub

    Private Sub BindDDLUserType()
        Me.ddlUserType.DataTextField = "Text"
        Me.ddlUserType.DataValueField = "Value"
        Me.ddlUserType.DataSource = MyObj.GetCodeMappingData("User", "UserType")
        Me.ddlUserType.DataBind()

        Me.ddlUserType.Items.Insert(0, MyObj.GetDDLSelectAllItem())

    End Sub

    Public Sub Show()
        Me.DataAccessMode = My.DataAccessMode.Update
        'Me.BindGV()
    End Sub

    Public Sub ShowReadOnly()
        Me.DataAccessMode = My.DataAccessMode.Read
        'Me.BindGV()
    End Sub

    Private Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyOrganizationDataFilter(Me.ProgramName)

        sql.AppendFormat("select u.UserID as OrderKey1, u.UserID, u.UserName, '' as RoleDescription ")
        sql.AppendFormat(", -1 as RoleID, -1 as RoleParent, null as RoleName, u.OrganizationID ")
        sql.AppendFormat("from FD_GetUserByURoot('{0}') u ", Me.ddlOrganization.SelectedValue)
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat(Me.GetWhereSQL())

        sql.AppendFormat("union all ")
        sql.AppendFormat("select u.UserID as OrderKey1, u.UserID, ru.RoleName, ru.RoleDescription ")
        sql.AppendFormat(", ru.RoleID as RoleID, r_parent.RoleID as RoleParent, ru.RoleName, u.OrganizationID ")
        sql.AppendFormat("from FD_RoleUserMappingView ru ")
        sql.AppendFormat("inner join FD_GetUserByURoot('{0}') u on ru.UserID = u.UserID ", Me.ddlOrganization.SelectedValue)
        sql.AppendFormat("left join FD_Role r_parent on ru.Parent = r_parent.RoleID ")
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat(Me.GetWhereSQL())

        'sql.AppendFormat(") a ")
        sql.AppendFormat("order by OrderKey1,RoleID ")
        'MyObj.WriteLog(sql.ToString())
        'Response.Write(sql.ToString())

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

        If Me.DataAccessMode = My.DataAccessMode.Read Then
            Me.gv.Columns(3).Visible = False
        End If
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        'If Me.IsOrganization = True Then
        '    sql.AppendFormat("where ot.Root = {0} ", Me.OrganizationRoot)
        'Else
        '    sql.AppendFormat("where ot.VRoot = {0} ", Me.VOrganizationRoot)
        'End If

        'If Me.ddlOrganization.SelectedValue <> "" Then
        '    sql.AppendFormat("where ot.URoot = '{0}' ", Me.ddlOrganization.SelectedValue)
        'End If

        If Me.ddlUserType.SelectedValue <> "" Then
            sql.AppendFormat("and u.UserType = '{0}' ", Me.ddlUserType.SelectedValue)
        End If

        If Me.txtUserID.Text <> "" Then
            sql.AppendFormat("and u.UserID like '%{0}%' ", Me.txtUserID.Text)
        End If

        If Me.txtUserName.Text <> "" Then
            sql.AppendFormat("and u.UserName like '%{0}%' ", Me.txtUserName.Text)
        End If

        Return sql.ToString()
    End Function

    Private Function SaveData(ByVal rowIndex As Integer) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Dim row As GridViewRow = Me.gv.Rows(rowIndex)
        Dim key As DataKey = Me.gv.DataKeys(rowIndex)

        Using scope As New Transactions.TransactionScope
            For Each roleID As String In Me.SearchRoleControl1.SelectedRoles
                sql.Length = 0
                sql.AppendFormat("insert FD_RoleUserMapping (RoleID, UserID) select ")
                sql.AppendFormat(" '{0}' ", roleID)
                sql.AppendFormat(",'{0}' ", key("UserID"))
                sql.AppendFormat(";insert FD_UserRoleMapping (RoleID, UserID) select ")
                sql.AppendFormat(" '{0}' ", roleID)
                sql.AppendFormat(",'{0}' ", key("UserID"))

                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            Next
            scope.Complete()
        End Using

        Return True
    End Function

    Private Function DeleteData(ByVal rowIndex As Integer) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Dim row As GridViewRow = Me.gv.Rows(rowIndex)
        Dim key As DataKey = Me.gv.DataKeys(rowIndex)

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("delete FD_RoleUserMapping where ")
            sql.AppendFormat(" RoleID='{0}' ", key("RoleID"))
            sql.AppendFormat("and UserID='{0}' ", key("UserID"))
            sql.AppendFormat(";delete FD_UserRoleMapping where ")
            sql.AppendFormat(" RoleID='{0}' ", key("RoleID"))
            sql.AppendFormat("and UserID='{0}' ", key("UserID"))

            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            scope.Complete()
        End Using

        Return True
    End Function

    Private ReadOnly Property IsOrganization() As Boolean
        Get
            If Me.ddlOrganization.SelectedValue.Split(",")(0) = "-1" Then
                Return False
            Else
                Return True
            End If
        End Get
    End Property

    Private ReadOnly Property OrganizationRoot() As String
        Get
            Return Me.ddlOrganization.SelectedValue.Split(",")(0)
        End Get
    End Property

    Private ReadOnly Property VOrganizationRoot() As String
        Get
            Return Me.ddlOrganization.SelectedValue.Split(",")(1)
        End Get
    End Property

    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)

    Private Property SelectedRowIndex() As Integer
        Get
            Return ViewState("SelectedRowIndex")
        End Get
        Set(ByVal value As Integer)
            ViewState("SelectedRowIndex") = value
        End Set
    End Property

    Public Property DataAccessMode() As My.DataAccessMode
        Get
            If ViewState("DataAccessMode") Is Nothing Then
                Return My.DataAccessMode.Update
            End If

            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property
End Class